package com.fortune.controller;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.fortune.common.paging.PagingData;
import com.fortune.constants.Constants;
import com.fortune.dto.excution.DSalesmanSettingExcution;
import com.fortune.dto.in.dSalesmanSetting.DSalesmanSettingListDto;
import com.fortune.dto.in.dUserMgt.DUserListDto;
import com.fortune.entity.DUserInfo;
import com.fortune.entity.MUserInfo;
import com.fortune.enums.DSalesmanSettingEnum;
import com.fortune.service.DSalesmanSettingService;
import com.fortune.vo.dSalesmanSetting.ProNum;
import com.fortune.vo.dSalesmanSetting.User;
import com.fortune.vo.dSelect.DSelectProduct;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fortune.base.BaseAPIExcution;
import com.fortune.dto.in.dSalesmanSetting.DSalesmanSettingAddDto;
import com.fortune.dto.in.dSalesmanSetting.DSalesmanSettingEditDto;
import com.fortune.vo.dSalesmanSetting.DSalesmanSettingInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Api(value = "dSalesmanSetting", description = "经销商业务员业绩设置", tags = {"经销商业务员业绩设置"})
@RestController
@RequestMapping("dSalesmanSetting")
public class DSalesmanSettingController {
	@Autowired
	private DSalesmanSettingService dSalesmanSettingService;

	@ApiOperation(value = "业绩设置员工列表", nickname = "dSalesmanSettingList", notes = "", tags={ "@安童" })
    @GetMapping(value = "list",
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<List<DSalesmanSettingInfo>> list(HttpServletRequest request) {
		HttpSession session = request.getSession();
		DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
		String did = dUserInfo.getdId();
		List<DSalesmanSettingInfo> list = dSalesmanSettingService.findUser(did);
		return new DSalesmanSettingExcution<List<DSalesmanSettingInfo>>(list,DSalesmanSettingEnum.SUCCESS);
	}

	@ApiOperation(value = "通过员工查业绩", nickname = "dSaleWhereUserId", notes = "", tags={ "@安童" })
	@ApiImplicitParams({
			@ApiImplicitParam(name="userId", value="员工ID",paramType="query",dataType="string"),
			@ApiImplicitParam(name="page", value="第几页",paramType="query",dataType="int"),
			@ApiImplicitParam(name="length", value="每页容量",paramType="query",dataType="int"),
	})
	@GetMapping(value = "dSaleWhereUserId",
			produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<PagingData<DSalesmanSettingInfo>> dSaleWhereUserId(HttpServletRequest request, @Validated @ApiIgnore DUserListDto dUserListDto,@Validated @NotBlank String userId) {
		HttpSession session = request.getSession();
		DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
		String did = dUserInfo.getdId();
		dUserListDto.setPage((dUserListDto.getPage()-1)*10);
		PagingData<DSalesmanSettingInfo> pagingData = dSalesmanSettingService.findSalesmanSettingList(did,userId,dUserListDto);
		/*List<DSalesmanSettingInfo> list = dSalesmanSettingService.findSalesmanSettingList(did,userId);*/
	/*	List<DSalesmanSettingInfo> list = dSalesmanSettingService.findSalesmanSettingList(did,userId);*/
		return new DSalesmanSettingExcution<PagingData<DSalesmanSettingInfo>>(pagingData,DSalesmanSettingEnum.SUCCESS);
	}

	@ApiOperation(value = "添加业绩设置", nickname = "dSalesmanSettingAdd", notes = "", tags={ "@安童" })
    @PostMapping(value = "add",
        produces = { MediaType.APPLICATION_JSON_VALUE },
        consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> add(@Validated @RequestBody DSalesmanSettingAddDto addDto) {
		return null;
	}
	
	@ApiOperation(value = "编辑业绩设置", nickname = "dSalesmanSettingEdit", notes = "", tags={ "@安童" })
	@ApiImplicitParams({
			@ApiImplicitParam(name="userId", value="员工ID",paramType="query",dataType="string"),
			@ApiImplicitParam(name="mid", value="厂商ID",paramType="query",dataType="string"),
			@ApiImplicitParam(name="mpid", value="商品ID",paramType="query",dataType="string"),
			@ApiImplicitParam(name="monthNum", value="业绩目标",paramType="query",dataType="string"),
	})
    @PutMapping(value = "edit",
        produces = { MediaType.APPLICATION_JSON_VALUE },
        consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> edit(HttpServletRequest request,@ApiIgnore @Validated DSalesmanSettingEditDto editDto) {
		HttpSession session = request.getSession();
		DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
		String did = dUserInfo.getdId();
		int num = dSalesmanSettingService.editMonthNum(editDto,did);
		if(num == 1){
			return new DSalesmanSettingExcution<Void>(null,DSalesmanSettingEnum.EDIT_SUCCESS);
		}
		return new DSalesmanSettingExcution<Void>(null,DSalesmanSettingEnum.EDIT_FAIL);
	}

	////////////////////////////////////////////////////////////////////////////////

	@ApiOperation(value = "业绩对比直方图", nickname = "performanceComparisonHistogram", notes = "", tags={ "@安童" })
	@ApiImplicitParams({
			@ApiImplicitParam(name="day", value="天数",paramType="query",dataType="int"),
	})
	@GetMapping(value = "performanceComparisonHistogram",
			produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Map> performanceComparisonHistogram(HttpServletRequest request,int day) {
		HttpSession session = request.getSession();
		DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
		String did = dUserInfo.getdId();
		List<DSalesmanSettingInfo> list = dSalesmanSettingService.findUser(did);   // 查user
		List<DSalesmanSettingInfo> lists = dSalesmanSettingService.findProduct(did);
		List<String> listUser = new ArrayList<>();
		List<Map<String,String>> lists2 = new ArrayList<>();
		for(DSalesmanSettingInfo dSalesmanSettingInfos:lists){
			Map map = new HashMap();
			map.put("product",dSalesmanSettingInfos.getProName());
			List<Float> list2 = new ArrayList<>();
			for(DSalesmanSettingInfo dSalesmanSettingInfo:list){
				Float aFloat = dSalesmanSettingService.findUserNum(dSalesmanSettingInfos.getMid(),dSalesmanSettingInfos.getMpid(),did,dSalesmanSettingInfo.getUserId(),day);
				list2.add(aFloat);
			}
			map.put("num",list2);
			lists2.add(map);
		}
		for(DSalesmanSettingInfo dSalesmanSettingInfo:list){
			listUser.add(dSalesmanSettingInfo.getName());
		}
		Map map = new HashMap();
		map.put("name",listUser);
		map.put("data",lists2);
		return new DSalesmanSettingExcution<Map>(map,DSalesmanSettingEnum.SUCCESS);
	}

	@ApiOperation(value = "员工业绩表", nickname = "employeePerformance", notes = "", tags={ "@安童" })
	@GetMapping(value = "employeePerformance",
			produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<List<User>> employeePerformance(HttpServletRequest request) {
		HttpSession session = request.getSession();
		DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
		String did = dUserInfo.getdId();
		int day = 0;
		List<User> list = dSalesmanSettingService.findUsers(did);   // 查user
		for(User user:list){
			List<ProNum> lists = dSalesmanSettingService.findSalesmanSettingLists(did,user.getUserId());
			user.setCount(lists.size());
			user.setPro(lists);
			for(ProNum proNum : user.getPro()){
				Float aFloat = dSalesmanSettingService.findUserNum(proNum.getMid(),proNum.getMpid(),did,user.getUserId(),day);
				proNum.setNum(aFloat);
				Float ss = Float.parseFloat(proNum.getMonthNum());
				int aa = Math.round(ss);
				if(aa == 0){
					proNum.setPercentage("无此业绩目标");
				}else{
					int as = Math.round(aFloat)/aa*100;
					String mm = Integer.toString(as)+"%";
					proNum.setPercentage(mm);
				}
			}
		}
		return new DSalesmanSettingExcution<List<User>>(list,DSalesmanSettingEnum.SUCCESS);
	}

	@ApiOperation(value = "查当前经销商下业务员", nickname = "userYWY", notes = "", tags={ "@安童" })
	@GetMapping(value = "userYWY",
			produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<List<DSalesmanSettingInfo>> userYWY(HttpServletRequest request) {
		HttpSession session = request.getSession();
		DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
		String did = dUserInfo.getdId();
		List<DSalesmanSettingInfo> list = dSalesmanSettingService.findUser(did);   // 查user
		return new DSalesmanSettingExcution<List<DSalesmanSettingInfo>>(list,DSalesmanSettingEnum.SUCCESS);
	}

	@ApiOperation(value = "业绩折线图", nickname = "performanceStatisticsLine", notes = "", tags={ "@安童" })
	@ApiImplicitParams({
			@ApiImplicitParam(name="day", value="天数",paramType="query",dataType="int"),
			@ApiImplicitParam(name="userId", value="业务员ID",paramType="query",dataType="string"),
	})
	@GetMapping(value = "performanceStatisticsLine",
			produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<List<ProNum>> performanceStatisticsLine(HttpServletRequest request,int day,String userId) {
		if(userId.equals("")){
			return new DSalesmanSettingExcution<List<ProNum>>(null,DSalesmanSettingEnum.SUCCESSFAIL);
		}
		HttpSession session = request.getSession();
		DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
		String did = dUserInfo.getdId();
		List<ProNum> lists = dSalesmanSettingService.findSalesmanSettingLists(did,userId);
		for(ProNum proNum:lists){
			List<Float> listDay = new ArrayList<>();
			for(int i=1; i<=day;i++){
				Float aFloat = dSalesmanSettingService.findUserNum(proNum.getMid(),proNum.getMpid(),did,userId,i);
				listDay.add(aFloat);
			}
			proNum.setDay(listDay);
		}

		return new DSalesmanSettingExcution<List<ProNum>>(lists,DSalesmanSettingEnum.SUCCESS);
	}
}
